package com.txhy.certificatechain.presenter.user;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.txhy.certificatechain.api.UrlAddress;
import com.txhy.certificatechain.presenter.base.BasePresenter;
import com.txhy.certificatechain.ui.view.user.UserView;
import com.txhy.certificatechain.utils.GsonUtil;

import java.io.File;

/*
 * -----------------------------------------------------------------
 * Copyright (C) 2018-2030, by your company, All rights reserved.
 * -----------------------------------------------------------------
 * 文    件: UserPresenter.java
 * 作    者: 侯建军
 * 版    权: 1.0.0
 * 描    述:
 * 创建时间: 2018/9/19 16:57
 * 更新时间：2018/9/19 16:57
 * -----------------------------------------------------------------
 * 2018/9/19 16:57: 创建 UserPresenter.java (侯建军);
 * -----------------------------------------------------------------
 */
public class UserPresenter extends BasePresenter<UserView> {

    public void upLoadImg(String filePath) {

        OkGo.<String>post(UrlAddress.BASE_IMGURL)//
                .tag(this)//
                .params("file", new File(filePath))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().upMsgSuccess(GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().upMsgFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().upMsgFail("上传失败，请重新上传");
                    }
                });
    }
    public void upDataImg(String userId,String newPath,String userType)  {

        OkGo.<String>post(UrlAddress.CHANGEAVATAR_URL)//
                .tag(this)//
                .params("userId",userId)
                .params("newPath",newPath)
                .params("userType",userType)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (GsonUtil.GsonGetStatus(response.body()) == 200) {
                            getView().upDataImgSuccess(GsonUtil.GsonGetData(response.body()));
                        } else {
                            getView().upDataImgFail(GsonUtil.GsonGetMsg(response.body()));
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        getView().upDataImgFail("更改失败，请重新上传");
                    }
                });
    }
}

